﻿using System;

namespace YueRu.Common
{
    /// <summary>
    /// 日期类型
    /// </summary>
    public enum DateType
    {
        /// <summary>
        /// 季度
        /// </summary>
        Quarter,

        /// <summary>
        /// 月
        /// </summary>
        Month,

        /// <summary>
        /// 周
        /// </summary>
        Week
    }

    /// <summary>
    /// 时间日期的帮助类
    /// </summary>
    public sealed class DateTimeHelper
    {
        /// <summary>
        /// 获取指定时间的unix时间戳
        /// </summary>
        /// <param name="dateTime">指定的时间日期</param>
        /// <returns>返回指定的时间的unix的时间戳</returns>
        public static long GetTimestamp(DateTime dateTime)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (long) (dateTime - startTime).TotalSeconds;
        }

        /// <summary>
        /// 获取当前时间的unix时间戳
        /// </summary>
        /// <returns>返回当前时间的unix时间戳</returns>
        public static long GetTimestamp()
        {
            return GetTimestamp(DateTime.Now);
        }

        public static DateTime GetDateTimeFromTimestamp(long timestamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timestamp.ToString() + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }

        /// <summary>
        /// 根据日期和类型获取第一天和最后一天的日期
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="type">日期类型</param>
        /// <returns></returns>
        public static DateTime[] GetFirstDayAndLastDay(DateTime date, DateType type)
        {
            if (date == DateTime.MinValue) return null;
            DateTime firstDayDate = DateTime.MinValue;
            DateTime lastDayDate = DateTime.MinValue;
            switch (type)
            {
                case DateType.Quarter:
                    var firstMonthInQuarter = date.AddMonths(-(date.Month - 1)%3);
                    firstDayDate = new DateTime(date.Year, firstMonthInQuarter.Month, 1);
                    lastDayDate = firstDayDate.AddMonths(3).AddDays(-1);
                    break;
                case DateType.Month:
                    var daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
                    firstDayDate =
                        new DateTime(date.Year, date.Month, 1);
                    lastDayDate =
                        new DateTime(date.Year, date.Month, daysInMonth);
                    break;
                case DateType.Week:
                    var dayOfWeek = (int) date.DayOfWeek;
                    firstDayDate = date.AddDays(-(dayOfWeek - 1));
                    lastDayDate = date.AddDays((7 - dayOfWeek));
                    break;
            }
            lastDayDate = new DateTime(lastDayDate.Year, lastDayDate.Month, lastDayDate.Day, 23, 59, 59);
            return new[]
            {
                firstDayDate, lastDayDate
            };
        }
    }
}